﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WeatherBuiltIn
{
   public class HaveaLoveaAffairExponent : IObserver<MeasurementData>
    {
        private float exponent = 0.0f;
        private IDisposable _unsubscriber;
        public virtual void Subscribe(IObservable<MeasurementData> provider)
        {
            if (provider != null)//判断provider不是空对象
                _unsubscriber = provider.Subscribe(this);
        }
        public void OnError(Exception error)
        {
            //var original = Console.ForegroundColor;
            //Console.ForegroundColor = ConsoleColor.Red;
            //Console.WriteLine("继续运行！");
            //Console.ForegroundColor = original;
        }
        public void OnNext(MeasurementData value)
        {
            exponent = ComputeHeatIndex(value.Temperature);
            Console.WriteLine(
                $"HaveaLoveaAffairExponent: " +
                $"\n恋爱指数为：{exponent}");
            Console.WriteLine();
        }
        public void OnCompleted()
        {
            Console.WriteLine("HaveaLoveaAffairExponent，恋爱指数 任务完成");
            Unsubscribe();
        }
        // 取消订阅
        public virtual void Unsubscribe()
        {
            Console.WriteLine("HaveaLoveaAffairExponent 取消订阅");
            _unsubscriber.Dispose();
        }
       private float ComputeHeatIndex(float t)
        {
            if (t < 1)
            {
                return 10f;
            }
            else if (t < 15 && t >= 1)
            {
                return 30f;
            }
            else if (t < 30 && t >= 15)
            {
                return 100f;
            }
            else if (t >= 30 && t < 45)
            {
                return 60f;
            }
            else
            {
                return 0f;
            }

        }
    }
}
